package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.ApartmentInfoMapper;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    // 查询房间数量，这个逻辑写在房间的组件类中更合适，判断的理由是：查询的是房间表
    @Autowired
    private RoomInfoService roomInfoService ;

    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private ApartmentFacilityService apartmentFacilityService;

    @Autowired
    private ApartmentLabelService apartmentLabelService ;

    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService ;


    @Override
    public IPage<ApartmentItemVo> pageItem(IPage page, ApartmentQueryVo queryVo) {
        return baseMapper.pageItem(page,queryVo);
    }

    @Override
    public ApartmentDetailVo getApartmentDetailVoById(Long id) {
        return baseMapper.getApartmentDetailVoById(id);
    }

    @Override
    public void removeApartmentById(Long id) {
        //1. 获取特定id对应的公寓的房间数。如果房间数大于0，则无法删除，抛出异常
        Long count = roomInfoService.count(new LambdaQueryWrapper<RoomInfo>().eq(RoomInfo::getApartmentId, id)
                .groupBy(RoomInfo::getApartmentId));

        //2. 如果房间数为0，则删除各种关联关系，然后再删除公寓本身的信息
        if(count>0){
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }else{
            //2-1. 删除图片关系
            graphInfoService
                    .remove(new LambdaUpdateWrapper<GraphInfo>()
                            .eq(GraphInfo::getItemType, ItemType.APARTMENT)
                            .eq(GraphInfo::getItemId,id));

            //2-2. 删除配套关系
            apartmentFacilityService
                    .remove(new LambdaUpdateWrapper<ApartmentFacility>()
                            .eq(ApartmentFacility::getApartmentId,id));


            //2-3. 删除标签关系
            apartmentLabelService
                    .remove(new LambdaUpdateWrapper<ApartmentLabel>()
                            .eq(ApartmentLabel::getApartmentId,id));

            //2-4. 删除杂费值关系
            apartmentFeeValueService
                    .remove(new LambdaUpdateWrapper<ApartmentFeeValue>()
                            .eq(ApartmentFeeValue::getApartmentId,id));

            //2-5. 删除公寓本身信息
            removeById(id);

        }
    }
}




